#include <include/graph_init.hpp>

GraphInit::GraphInit(){}

int GraphInit::graphLoad(string file, Graph * graph ) {
    //representation
    int label = 0;
    int numberOfVertices = 0;
    string line;

    //open file for reading
    std::ifstream fileDst(file, std::ifstream::in);

    //control if file is open
    if(fileDst.is_open())
    {
        std::vector<Vertex*> * listOfVertices = graph->getListOfVertices();

        //process file line by line
        while(getline(fileDst,line))
        {
            if(numberOfVertices == 0) {
                numberOfVertices = std::stoi(line);
                graph->setNumberOfVertices(numberOfVertices);
                graph->graphInVector.push_back(numberOfVertices);

                for(int i = 0; i < numberOfVertices; ++i) {
                    graph->addVertex(new Vertex(i));
                }

                continue;
            }


            //find all edges with other vertices
            for(int i = label; line.length() > i; i++)
            {
                graph->graphInVector.push_back(line[i]);
                if(line[i] == '1')
                {
                    Vertex * firstVertex = listOfVertices->at(label);
                    Vertex * secondVertex = listOfVertices->at(i);

                    Edge *actualEdge = new Edge(firstVertex, secondVertex);
                    graph->addEdge(actualEdge);

                    firstVertex->addNeighbour(secondVertex);
                    secondVertex->addNeighbour(firstVertex);
                }

            }

            label++;
        }

    }
    else
    {
        cerr << "File with graph is unable to open.";
    }


    return 0;

}
